package com.bella.api.utils;

import java.io.Serializable;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;

/**
 * Restful API 返回JSON结果: { "code":0, "message":"", "errors": [ErrorEntity],
 * "data":T }
 * 
 * @author guxiangguo
 */
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(Include.NON_NULL)
public class ApiResponse<T> implements Serializable {

	private static final long serialVersionUID = -8706046120335187221L;

	/**
	 * 指定code
	 */
	public ApiResponse(ApiResponseCode status) {
		this(status.getCode(), status.getMessage());
	}

	public ApiResponse(Integer code) {
		this(code, null, null, null);
	}

	public ApiResponse(Integer code, String message) {
		this(code, message, null, null);
	}

	public ApiResponse(Integer code, String message, T data) {
		this(code, message, null, data);
	}

	public ApiResponse(Integer code, String message, List<ErrorEntity> errors, T data) {
		this.code = code;
		this.message = message;
		this.errors = errors;
		this.data = data;
		this.sysDatetime = System.currentTimeMillis();
	}

	public ApiResponse() {
	}

	public ApiResponse(ApiResponseCode sucess, T no) {
		this.code = sucess.getCode();
		this.message = sucess.getMessage();
		this.data =  no;
		this.sysDatetime = System.currentTimeMillis();
	}

	/** 系统状态 */
	@JsonProperty("code")
	private Integer code;

	/** 系统消息信息 */
	@JsonProperty("message")
	private String message;

	/** 错误内容列表 */
	@JsonProperty("errors")
	private List<ErrorEntity> errors;

	/** 返回结果内容 */
	@JsonProperty("data")
	private T data;

	private Long sysDatetime;

	public Integer getCode() {
		return code;
	}

	public void setCode(Integer code) {
		this.code = code;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public List<ErrorEntity> getErrors() {
		return errors;
	}

	public void setErrors(List<ErrorEntity> errors) {
		this.errors = errors;
	}

	public T getData() {
		return data;
	}

	public void setData(T data) {
		this.data = data;
	}

	/**
	 * @return the sysDatetime
	 */
	public Long getSysDatetime() {
		return sysDatetime;
	}

	/**
	 * @param sysDatetime the sysDatetime to set
	 */
	public void setSysDatetime(Long sysDatetime) {
		this.sysDatetime = sysDatetime;
	}

}
